local KKB_Physgun_ErrorCodes = {
	"Something Wierd Happened",
	"Physgun Can't Touch World",
	"Physgun Can't Touch World Props",
	"Vehicle Can't Be Physgunned",
	"Weapon Can't Be Physgunned",
	"NPC Can't Be Physgunned",
	"Entity Can't Be Physgunned",
	"Other Can't Be Physgunned",
	"Not On Buddies List"
}

local function KKB_CanPhysgun_IgnoreBuddys( Group1, Group2 )
	local IgnoreBuddys = false
	if ( KKB.UserGroups[Group1].Other.IgnoreBuddys or KKB.UserGroups[Group2].Other.IgnoreBuddys ) then
		IgnoreBuddys = true
	end
	return IgnoreBuddys
end

function KKB.PhysgunPickup( Ply, Entity )
	local UCL, CustomUser = KKB.GetPlayerGroups( Ply )
	local OwnerID = Entity.KKB_OwnerID
	local ErrorCode = 1
	local CanPhysgun = false
	if ( OwnerID == nil ) then
		--Dealing With A World Something
		if ( Entity:IsWorld() ) then
			--The Actual World
			ErrorCode = 2
			--Check If UserGroup Can Always Touch World
			if ( KKB.UserGroups[UCL].Physgun.World or KKB.UserGroups[CustomUser].Physgun.World ) then
				CanPhysgun = true
			end
		else
			--A World Entity
			ErrorCode = 3
			--Check If UserGroup Can Always Touch WorldProps
			if ( KKB.UserGroups[UCL].Physgun.WorldProps or KKB.UserGroups[CustomUser].Physgun.WorldProps ) then
				CanPhysgun = true
			end
		end
	else
		local UniqueID = Ply:UniqueID()
		if ( OwnerID != nil and OwnerID != UniqueID ) then
			local IgnoreBuddys = KKB_CanPhysgun_IgnoreBuddys( UCL, CustomUser )
			if ( not IgnoreBuddys ) then
				--if ( KKB_BuddysCheck( Entity.KKB_OwnerID, UniqueID ) ) then
					ErrorCode = 9
					CanPhysgun = false
					return CanPhysgun, ErrorCode
				--end
			end
		end
		--Dealing With A Player Something
		local Class = Entity:GetClass()
		if ( Entity:IsVehicle() ) then
			local Restriction = KKB.VehicleRestrictions[Class]
			ErrorCode = 4
			if ( Restriction != nil ) then
				CanPhysgun = Restriction.Options.Physgun
			end
			if ( not CanPhysgun ) then
				if ( KKB.UserGroups[UCL].Physgun.Vehicles or KKB.UserGroups[CustomUser].Physgun.Vehicles ) then
					CanPhysgun = true
				end
			end
		elseif ( Entity:IsWeapon() ) then
			local Restriction = KKB.WeaponRestrictions[Class]
			ErrorCode = 5
			if ( Restriction != nil ) then
				CanPhysgun = Restriction.Options.Physgun
			end
			if ( not CanPhysgun ) then
				if ( KKB.UserGroups[UCL].Physgun.Weapons or KKB.UserGroups[CustomUser].Physgun.Weapons ) then
					CanPhysgun = true
				end
			end
		elseif ( Entity:IsNPC() ) then
			local Restriction = KKB.NPCRestrictions[Class]
			ErrorCode = 6
			if ( Restriction != nil ) then
				CanPhysgun = Restriction.Options.Physgun
			end
			if ( not CanPhysgun ) then
				if ( KKB.UserGroups[UCL].Physgun.NPCs or KKB.UserGroups[CustomUser].Physgun.NPCs ) then
					CanPhysgun = true
				end
			end
		else
			local Restriction = KKB.EntityRestrictions[Class]
			ErrorCode = 7
			if ( Restriction != nil ) then
				CanPhysgun = Restriction.Options.Physgun
				if ( not CanPhysgun ) then
					if ( KKB.UserGroups[UCL].Physgun.Entitys or KKB.UserGroups[CustomUser].Physgun.Entitys ) then
						CanPhysgun = true
					end
				end
			end
			--I'm Not Quite Sure I Made An 'Other' Check
			--It's Quite Redundant But There May Be A Purpose
			--The Other Errors Are Going To Pop Up If
			--Its An Entity That's Never Been Set
			--I Suppose This Will Cause A Little Confusion
			--But I Don't See An Easy Way Around It
			--As If Any Of This Was Easy
			--Insert Eye Roll
			if ( Restriction == nil ) then
				ErrorCode = 8
				if ( not CanPhysgun ) then
					if ( KKB.UserGroups[UCL].Physgun.Other or KKB.UserGroups[CustomUser].Physgun.Other ) then
						CanPhysgun = true
					end
				end
			end
		end
	end
	return CanPhysgun, ErrorCode
end

local function KKB_PhysgunPickup( Arg1, Arg2 )
	local CanPhysgun = false
	local Ply = nil
	local Ent = nil
	local ErrorCode = 1
	if ( Arg1:IsPlayer() ) then
		Ply = Arg1
		Ent = Arg2
	else
		Ply = Arg2
		Ent = Ply:GetEyeTrace().Entity
	end
	if ( IsValid(Ply) and IsValid(Ent) ) then
		CanPhysgun, ErrorCode = KKB.PhysgunPickup( Ply, Ent )
	end
	--Make Sure We Only Ever Return False
	if ( not CanPhysgun ) then
		Ply:SendLua("GAMEMODE:AddNotify(\""..KKB_Physgun_ErrorCodes[ErrorCode].."\", NOTIFY_ERROR, 10)")
		return CanPhysgun
	end
end
hook.Add("PhysgunPickup", "KKB_PhysgunPickup", KKB_PhysgunPickup)
hook.Add("OnPhysgunReload", "KKB_OnPhysgunReload", KKB_PhysgunPickup)